/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.portalnahouby.implementation.util;

import cz.muni.fi.pa165.portalnahouby.api.dto.ForestDTO;
import cz.muni.fi.pa165.portalnahouby.api.dto.MushroomDTO;
import cz.muni.fi.pa165.portalnahouby.api.dto.PickerDTO;
import cz.muni.fi.pa165.portalnahouby.api.dto.VisitDTO;
import cz.muni.fi.pa165.portalnahouby.implementation.entity.Forest;
import cz.muni.fi.pa165.portalnahouby.implementation.entity.Mushroom;
import cz.muni.fi.pa165.portalnahouby.implementation.entity.Picker;
import cz.muni.fi.pa165.portalnahouby.implementation.entity.Visit;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Lukáš Mařica
 */
public class EntityDTOConvertor {

    public static Forest convertForest(ForestDTO forest) {
        Forest forestEntity = new Forest();
        forestEntity.setId(forest.getId());
        forestEntity.setInfo(forest.getInfo());

        List<Mushroom> mushroomList = new LinkedList<Mushroom>();
        for (MushroomDTO mushroom : forest.getMushrooms()) {
            mushroomList.add(EntityDTOConvertor.convertMushroom(mushroom));
        }

        forestEntity.setMushrooms(mushroomList);
        forestEntity.setName(forest.getName());

        return forestEntity;
    }

    public static ForestDTO convertForest(Forest forest) {

        List<MushroomDTO> dtoList = new LinkedList<MushroomDTO>();
        for (Mushroom mushroom : forest.getMushrooms()) {
            dtoList.add(EntityDTOConvertor.convertMushroom(mushroom));
        }

        return new ForestDTO(forest.getId(), forest.getName(), forest.getInfo(), dtoList);
    }

    public static Mushroom convertMushroom(MushroomDTO mushroom) {
        Mushroom mushroomEntity = new Mushroom();

        mushroomEntity.setId(mushroom.getId());
        mushroomEntity.setName(mushroom.getName());
        mushroomEntity.setEdibility(mushroom.getEdibility());
        mushroomEntity.setGrowFrom(mushroom.getGrowFrom());
        mushroomEntity.setGrowTo(mushroom.getGrowTo());

        return mushroomEntity;
    }

    public static MushroomDTO convertMushroom(Mushroom mushroom) {
        return new MushroomDTO(mushroom.getId(), mushroom.getName(), mushroom.getEdibility(),
                mushroom.getGrowFrom(), mushroom.getGrowTo());
    }

    public static Picker convertPicker(PickerDTO picker) {
        Picker pickerEntity = new Picker();
        pickerEntity.setId(picker.getId());
        pickerEntity.setInfo(picker.getInfo());
        pickerEntity.setIsAdmin(picker.getIsAdmin());
        pickerEntity.setName(picker.getName());
        pickerEntity.setSurname(picker.getSurname());
        pickerEntity.setLogin(picker.getLogin());
        pickerEntity.setPass(picker.getPass());

        return pickerEntity;
    }

    public static PickerDTO convertPicker(Picker picker) {
        if(picker == null) 
        {
    	    return null;
        }
        PickerDTO pickerDto = new PickerDTO();
        pickerDto.setId(picker.getId());
        pickerDto.setInfo(picker.getInfo());
        pickerDto.setIsAdmin(picker.getIsAdmin());
        pickerDto.setName(picker.getName());
        pickerDto.setSurname(picker.getSurname());
        pickerDto.setLogin(picker.getLogin());
        pickerDto.setPass(picker.getPass());

        return pickerDto;
    }

    public static Visit convertVisit(VisitDTO visit) {
        Visit visitEntity = new Visit();
        visitEntity.setDescription(visit.getDescription());
        visitEntity.setForest(EntityDTOConvertor.convertForest(visit.getForest()));
        visitEntity.setId(visit.getId());
        visitEntity.setPicker(EntityDTOConvertor.convertPicker(visit.getPicker()));
        visitEntity.setVisitDate(visit.getVisitDate());

        return visitEntity;
    }

    public static VisitDTO convertVisit(Visit visit) {
        VisitDTO visitDto = new VisitDTO();
        visitDto.setDescription(visit.getDescription());
        visitDto.setForest(EntityDTOConvertor.convertForest(visit.getForest()));
        visitDto.setId(visit.getId());
        visitDto.setPicker(EntityDTOConvertor.convertPicker(visit.getPicker()));
        visitDto.setVisitDate(visit.getVisitDate());

        return visitDto;
    }
}
